
#include "stdafx.h"

TEST(acceptTest, accept_in_inout)
{
    SOCKET sock = INVALID_SOCKET;
    int len = 11;
    
    Header header(GetCurrentThreadId(), "accept");
    accept_in_inout acceptIN(sock, &len);
    accept_in_inout acceptOUT;

    std::vector<char> buffer;
    Serialize(header, acceptIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(acceptOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (acceptIN.s_, acceptOUT.s_);
    if (acceptIN.addrlen_ && acceptOUT.addrlen_)
    {
        EXPECT_EQ (*acceptIN.addrlen_, *acceptOUT.addrlen_);
    }
}

TEST(acceptTest, accept_in_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;
    
    Header header(GetCurrentThreadId(), "accept");
    accept_in_inout acceptIN(sock, NULL);
    accept_in_inout acceptOUT;

    std::vector<char> buffer;
    Serialize(header, acceptIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(acceptOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (acceptIN.s_, acceptOUT.s_);
    if (acceptIN.addrlen_ && acceptOUT.addrlen_)
    {
        EXPECT_EQ (*acceptIN.addrlen_, *acceptOUT.addrlen_);
    }
}

TEST(acceptTest, accept_out_inout)
{
    SOCKET sock = INVALID_SOCKET;
    
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(27015);

    int len = sizeof(addr);

    Header header(GetCurrentThreadId(), "accept");
    accept_out_inout acceptIN(sock, (SOCKADDR*)&addr, &len, 7);
    accept_out_inout acceptOUT;

    std::vector<char> buffer;
    Serialize(header, acceptIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(acceptOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (acceptIN.ret_, acceptOUT.ret_);
    if (acceptIN.addr_)
    {
        EXPECT_EQ (acceptIN.addr_->sa_family, acceptOUT.addr_->sa_family);
        int cmp = strncmp (acceptIN.addr_->sa_data, acceptOUT.addr_->sa_data, 14);
        EXPECT_TRUE (!cmp);
    }
    if (acceptIN.addrlen_ && acceptOUT.addrlen_)
    {
        EXPECT_EQ (*acceptIN.addrlen_, *acceptOUT.addrlen_);
    }
    EXPECT_EQ (acceptIN.wsa_last_error_, acceptOUT.wsa_last_error_);
}

TEST(acceptTest, accept_out_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;

    Header header(GetCurrentThreadId(), "accept");
    accept_out_inout acceptIN(sock, NULL, NULL, 7);
    accept_out_inout acceptOUT;

    std::vector<char> buffer;
    Serialize(header, acceptIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(acceptOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (acceptIN.ret_, acceptOUT.ret_);
    if (acceptIN.addr_)
    {
        EXPECT_EQ (acceptIN.addr_->sa_family, acceptOUT.addr_->sa_family);
        int cmp = strncmp (acceptIN.addr_->sa_data, acceptOUT.addr_->sa_data, 14);
        EXPECT_TRUE (!cmp);
    }
    if (acceptIN.addrlen_ && acceptOUT.addrlen_)
    {
        EXPECT_EQ (*acceptIN.addrlen_, *acceptOUT.addrlen_);
    }
    EXPECT_EQ (acceptIN.wsa_last_error_, acceptOUT.wsa_last_error_);
}